import { Component, OnInit } from '@angular/core';
import { UserService } from '../../service/user.service';
import { NzMessageService } from 'ng-zorro-antd';
import { CommonService } from 'src/app/service/common.service';
import { ComponentBasic } from '../../app.component';
import { ActivatedRoute, Router } from '@angular/router';
import { RoleRight } from 'src/kalend';

@Component({
  selector: 'app-userlist',
  templateUrl: './userlist.component.html',
  styleUrls: ['./userlist.component.scss'],
  providers: [UserService, CommonService]
})
export class UserlistComponent extends ComponentBasic implements OnInit {
  isadmin = RoleRight.getisRole(null, '系统管理员');
  name: any;
  pageIndex = 1;
  pageSize = 15;
  total = 1;
  dataSet = [];
  loading = true;
  isOkLoading = false;
  isVisible = false;
  isVisible2 = false;
  isVisible89 = false;
  qrcodepath = '';
  sdate: any;
  edate: any;
  usdate: any;
  uedate: any;
  status: any;
  isup = false;
  issuper = false;
  isdiamond = false;
  getgift = false;
  mid: any;
  doc: any = {};
  inveritems: Array<any> = [];
  statuslist = [{ key: null, value: '全部' }, { key: 1, value: '有效' }, { key: 0, value: '禁用' }];
  userid = RoleRight.getStorage()._id;
  isrole = RoleRight.getisRole(null, '品牌宣传大使');

  constructor(private userservice: UserService, private msg: NzMessageService,
    private commonservice: CommonService, private router: Router) {
    super();
    super.setLayout(true, true);
  }

  ngOnInit() {
    this.searchData();
  }
  searchData(reset: boolean = false): void {
    this.loading = true;
    let doc: any = {
      offset: this.pageIndex,
      limit: this.pageSize,
      name: this.name,
      mid: this.mid,
      status: this.status,
      regstarttime: this.sdate,
      regendtime: this.edate,
      upstarttime: this.usdate,
      upendtime: this.uedate,
      isup: this.isup,
      issuper: this.issuper,
      isdiamond: this.isdiamond,
      getgift: this.getgift
    };
    if (this.isrole) {
      doc.userid = this.userid;
    }

    this.userservice.query(doc).then((res) => {
      this.loading = false;
      this.total = res.total;
      this.dataSet = res.rows;
    });
  }
  export() {
    let doc: any = {
      offset: this.pageIndex,
      limit: this.pageSize,
      name: this.name,
      mid: this.mid,
      status: this.status,
      regstarttime: this.sdate,
      regendtime: this.edate,
      upstarttime: this.usdate,
      upendtime: this.uedate,
      isup: this.isup,
      issuper: this.issuper,
      isdiamond: this.isdiamond,
      getgift: this.getgift
    };
    if (this.isrole) {
      doc.userid = this.userid;
    }
    this.userservice.export(doc);
  }
  /**禁用 */
  lock(id: any) {
    this.commonservice.confirm('确认禁用吗？', () => {
      this.userservice.lock(id).then((res) => {
        this.searchData();
      });
    });
  }
  /**启用 */
  open(id: any) {
    this.commonservice.confirm('确认启用吗？', () => {
      this.userservice.open(id).then((res) => {
        this.searchData();
      });
    });
  }
  /**设置发放人 */
  setsender(id: any) {
    this.commonservice.confirm('确认设置为礼品发放人吗？', () => {
      this.userservice.setsender(id).then((res) => {
        this.searchData();
      });
    });
  }
  /**设置发放人 */
  getQrcode(id: any) {
    this.isVisible89 = true;
    this.userservice.getqrcode({ userid: id }).then((res) => {
      this.qrcodepath = res;
    });

  }

  /**取消发放人 */
  nosender(id: any) {
    this.commonservice.confirm('确认取消礼品发放人吗？', () => {
      this.userservice.nosender(id).then((res) => {
        this.searchData();
      });
    });
  }
  /** 酒币账单 */
  winelist(id: any) {
    this.router.navigate(['/salesmanage/winelistUid', id]);
  }
  /** 消费账单 */
  orderlist(id: any) {
    this.router.navigate(['/salesmanage/orderlistUid', id]);
  }
  /** 邀请记录 */
  inverlist(id: any) {
    this.router.navigate(['/sysset/inverhistory', id]);
  }
  upwine(id: any, colnum: number) {
    this.isVisible = true;
    this.doc.num = colnum;
    this.doc.id = id;
    this.doc.uptype = 'add';
  }
  upwine2(id: any, colnum: number) {
    this.isVisible2 = true;
    this.doc.num = colnum;
    this.doc.id = id;
    this.doc.uptype = 'add';
  }
  handleOk(): void {
    this.isOkLoading = true;
    if (!this.doc.uptype) {
      this.msg.warning('请选择变更类型');
      // tslint:disable-next-line:radix
    } else if (parseInt(this.doc.number).toString() === 'NaN') {
      this.msg.warning('请输入正确的酒币数');
    } else if (this.doc.number == '0') {
      this.msg.warning('变更酒币数不能为0');
    } else if (this.doc.uptype === 'clu' && Number(this.doc.number) > Number(this.doc.num)) {
      this.msg.warning('减少酒币数不能大于持有数');
    } else {
      this.doc.userid = this.userid;
      this.doc.utype = '酒币';
      this.userservice.upwine(this.doc).then((res) => {
        this.searchData();
        this.isVisible = false;
        this.doc = {};
      });
    }
  }
  handleCancel(): void {
    this.isVisible = false;
    this.doc = {};
  }
  handleOk2(): void {
    this.isOkLoading = true;
    if (!this.doc.uptype) {
      this.msg.warning('请选择变更类型');
      // tslint:disable-next-line:radix
    } else if (parseInt(this.doc.number).toString() === 'NaN') {
      this.msg.warning('请输入正确的餐币数');
    } else if (this.doc.number == '0') {
      this.msg.warning('变更餐币数不能为0');
    } else if (this.doc.uptype === 'clu' && Number(this.doc.number) > Number(this.doc.num)) {
      this.msg.warning('减少餐币数不能大于持有数');
    } else {
      this.doc.userid = this.userid;
      this.doc.utype = '餐币';
      this.userservice.upwine(this.doc).then((res) => {
        this.searchData();
        this.isVisible2 = false;
        this.doc = {};
      });
    }
  }
  handleCancel2(): void {
    this.isVisible2 = false;
    this.doc = {};
  }

  handleCancel89(): void {
    this.isVisible89 = false;
    this.qrcodepath = "";
  }
}
